package pl.edu.agh.nn.main;

import java.io.File;

import pl.edu.agh.nn.conf.InvalidNetworkConfigurationException;
import pl.edu.agh.nn.net.INet;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String inputFile = null;

		try {
			inputFile = args[0];
		} catch (ArrayIndexOutOfBoundsException e) {
			// ignore
		}

		if (inputFile == null) {
			System.err.println("A file with net structure needs to be specified.");
			return;
		}

		StaxDriver driver = new StaxDriver();
		XStream xstream = new XStream(driver);
		xstream.aliasPackage("", "pl.edu.agh.nn");
		xstream.setMode(XStream.ID_REFERENCES);

		try {

			INet net = (INet) xstream.unmarshal(driver.createReader(new File(inputFile)));
			net.configure();
			if (net.getConfiguration() == null) {
				System.err.println("No network configuration specified.");
				return;
			}
			net.learn(net.getConfiguration().getTrainingCaseProvider());
			net.compute(net.getConfiguration().getInputDataProvider(), net.getConfiguration().getOutputDataProducer());

		} catch (InvalidNetworkConfigurationException e) {
			System.err.println("Invalid network configuration");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			System.err.println("Invalid format of input file");
		} catch (ArrayIndexOutOfBoundsException e) {
			System.err.println("Input neurons count and input values count does not match");
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
